package com.talking.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.talking.App;
import com.talking.databinding.ActivityLoginBinding;
import com.talking.presenter.UserPresenter;
import com.talking.utils.Utils;
import com.talking.view.main.MainActivity;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {
    private ActivityLoginBinding binding;
    private ActivityResultLauncher<Intent> launcher;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.loginLogo.setTypeface(Typeface.createFromAsset(getAssets(),"FZSJ-QIYXRYY.TTF"));
        launcher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode()==1){
                UserPresenter.login(App.sp.getString("account", ""), App.sp.getString("password", ""), new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        LoginActivity.this.runOnUiThread(() -> Utils.toast( e.toString()));
                    }
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String msg=response.body().string();
                        if (msg.equals("验证成功")){
                            LoginActivity.this.startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            LoginActivity.this.finish();
                        }else {
                            LoginActivity.this.runOnUiThread(() -> Utils.toast( msg));
                        }
                    }
                });
            }
        });
        setTitle("登陆");
        autoLoginCheck();
    }
    private void autoLoginCheck() {
        if (App.sp.getBoolean("autoLogin",false)){
            UserPresenter.login(App.sp.getString("account", ""), App.sp.getString("password", ""), new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    LoginActivity.this.runOnUiThread(() -> Utils.toast( e.toString()));
                }
                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    String msg=response.body().string();
                    if (msg.equals("验证成功")){
                        LoginActivity.this.startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        LoginActivity.this.finish();
                    }else {
                        LoginActivity.this.runOnUiThread(() -> Utils.toast( msg));
                    }
                }
            });
        }
    }
    public void sign(View view) {
        startActivity(new Intent(this, SignActivity.class));
    }
    public void forgotPassword(View view) {
        launcher.launch(new Intent(this, ForgotPasswordActivity.class));
    }
    public void login(View view){
        UserPresenter.login(binding.loginAccount.getText().toString(), binding.loginPassword.getText().toString(), new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                LoginActivity.this.runOnUiThread(() -> Utils.toast( e.toString()));
            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String msg=response.body().string();
                if (msg.equals("验证成功")){
                    UserPresenter.getUserByAccount(binding.loginAccount.getText().toString(),null, new Callback() {
                        @Override
                        public void onFailure(@NotNull Call call, @NotNull IOException e) {
                            LoginActivity.this.runOnUiThread(() -> Utils.toast( e.toString()));
                        }
                        @Override
                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                            try {
                                JSONObject jo=new JSONObject(response.body().string());
                                @SuppressLint("CommitPrefEdits") SharedPreferences.Editor edit = App.sp.edit();
                                edit.putBoolean("autoLogin", binding.loginAotoLogin.isChecked());
                                edit.putString("account",binding.loginAccount.getText().toString() );
                                edit.putString("password", binding.loginPassword.getText().toString());
                                edit.putString("email", jo.getString("email"));
                                edit.apply();
                                LoginActivity.this.startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                LoginActivity.this.finish();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }else {
                    LoginActivity.this.runOnUiThread(() -> Utils.toast( msg));
                }
            }
        });
    }
}